package com.dcd.controller;

import com.dcd.pojo.Information;
import com.dcd.service.InformationService;
import com.dcd.util.ExcelFileUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/InformationController")
@CrossOrigin
public class InformationController {
    @Autowired
    private InformationService informationService;

    /**
     * 查询全部资讯
     *
     * @return 返回所有资讯
     */
    @RequestMapping("/my_information_all/{pageZiXunIndex}")
    public Map<String, Object> my_information_all(@PathVariable Integer pageZiXunIndex) {
        return informationService.Information_all(pageZiXunIndex);
    }

    /**
     * 根据资讯id查询单个资讯详情
     * @return 返回根据资讯id查询出来的一个对象
     */
    @RequestMapping("/singleInformation/{information_id}")
    public Information singleInformation(@PathVariable Integer information_id) {
        return informationService.singleInformation(information_id);
    }

    /**
     * 查询前十的热搜榜
     *
     * @return 返回前十的数据
     */
    @RequestMapping("/tenInfoAll")
    public List<Information> tenInfoAll() {
        return informationService.tenInfoAll();
    }

    /**
     * 查询首页最新的24条资讯
     *
     * @return 返回最新的24条资讯
     */
    @RequestMapping("/select_index_information")
    public List<Information> select_index_information() {
        return informationService.select_index_information();
    }

    /**
     * 根据车系id查询此系列的所有资讯
     *
     * @param car_series_id 获取车系id
     * @return 返回同车系所有资讯
     */
    @RequestMapping("/seleCarSeriesInformation/{car_series_id}")
    public List<Information> seleCarSeriesInformation(@PathVariable Integer car_series_id) {
        return informationService.seleCarSeriesInformation(car_series_id);
    }

    /**
     * 搜索查询资讯
     *
     * @param solr_content 搜索文本内容
     * @param page_index   当前页数
     * @return
     */
    @RequestMapping("/select_information_by_solr/{solr_content}/{page_index}")
    public Map<String, Object> select_information_by_solr(@PathVariable String solr_content, @PathVariable int page_index) throws IOException, SolrServerException {
        return informationService.select_information_by_solr(solr_content, page_index);
    }
    /**
     * 查询最近一周的最新资讯
     * @return
     */
    @RequestMapping("/select_week")
    public List<Information> select_week(){
        return informationService.select_week();
    }

    public void getDownExcel(HttpServletResponse response, HttpServletRequest request,@PathVariable Integer pageZiXunIndex) {

//        //这个map使数据库生成的
//        Map<String, Object> map= informationService.Information_all(pageZiXunIndex);
//        map.get("");
//        // 得到结果，生成Excel文件
//        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//        HSSFSheet sheet = hssfWorkbook.createSheet("统计表");
//        // 生成表头
//        HSSFRow headRow = sheet.createRow(0);
//        headRow.createCell(0).setCellValue("表头1");
//        headRow.createCell(1).setCellValue("表头2");
//        headRow.createCell(2).setCellValue("表头3");
//        headRow.createCell(3).setCellValue("表头4");
//        headRow.createCell(4).setCellValue("表头5");
//
//        // 将数据插入表中
//        for (List<Information> list : map.get("")) {
//            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
//
//            //日期格式转换
//            Date date = new Date(info.getDate());
//            SimpleDateFormat sdFmt = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
//            dataRow.createCell(0).setCellValue(sdFmt.format(date));
//
//            dataRow.createCell(1).setCellValue(info.getUm());
//            dataRow.createCell(2).setCellValue(info.getUm());
//            dataRow.createCell(3).setCellValue(info.getUm());
//            dataRow.createCell(4).setCellValue(info.getUm());
//        }
//
//        String filename = "资讯统计表.xls";
//       ExcelFileUtils.excelExport(response, request, hssfWorkbook, filename);

    }



}
